package cn.zsb.ui;

import javax.swing.*;
import javax.swing.border.BevelBorder;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;

import static javax.swing.border.BevelBorder.RAISED;

public class GameJFrame extends JFrame implements KeyListener {

    int[][] data = new int[4][4];

    public GameJFrame() {

        // 初始化窗口
        initJFrame();

        // 初始化菜单栏
        initJMenuBar();

        // 初始化数据
        initData();

        // 初始化图片
        initImage();

        //设置窗口可见
        this.setVisible(true);

    }

    //记录空白模块在二维数组中的位置
    int x, y;

    private void initData() {
        int[] tempArr = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
        Random r = new Random();
        for (int i = 0; i < tempArr.length; i++) {
            int index = r.nextInt(tempArr.length);
            int temp = tempArr[i];
            tempArr[i] = tempArr[index];
            tempArr[index] = temp;
        }

        for (int i = 0; i < tempArr.length; i++) {
            if (tempArr[i] == 0) {
                x = i / 4;
                y = i % 4;
            }else {
                data[i / 4][i % 4] = tempArr[i];
            }
        }

    }

    private void initImage() {

        this.getContentPane().removeAll();

        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                int num = data[i][j];
                JLabel jLabel1 = new JLabel(new ImageIcon("puzzlegame\\image\\girl\\girl2\\" + num + ".jpg"));
                jLabel1.setBounds(105 * j + 83, 105 * i + 134, 105, 105);

                jLabel1.setBorder(new BevelBorder(RAISED));

                this.getContentPane().add(jLabel1);

            }
        }

        JLabel background = new JLabel(new ImageIcon("puzzlegame\\image\\background.png"));
        background.setBounds(40, 40, 508, 560);
        this.getContentPane().add(background);

        this.getContentPane().repaint();
    }

    private void initJFrame() {
        //设置窗口大小
        this.setSize(603, 680);
        //设置窗口标题
        this.setTitle("拼图单机版 v.1.0");
        //设置窗口始终置顶
        this.setAlwaysOnTop(true);
        //设置窗口位置
        this.setLocationRelativeTo(null);
        //设置窗口关闭操作
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setLayout(null);
        this.addKeyListener(this);
    }

    private void initJMenuBar() {//初始化菜单
        //菜单对象
        JMenuBar jMenuBar = new JMenuBar();
        JMenu functionJMenu = new JMenu("功能");
        JMenu aboutJMenu = new JMenu("关于我们");

        //条目对象
        JMenuItem replayItem = new JMenuItem("重新游戏");
        JMenuItem reLoginItem = new JMenuItem("重新登录");
        JMenuItem closeItem = new JMenuItem("关闭游戏");

        JMenuItem accountItem = new JMenuItem("公众号");

        //添加条目到菜单
        functionJMenu.add(replayItem);
        functionJMenu.add(reLoginItem);
        functionJMenu.add(closeItem);

        aboutJMenu.add(accountItem);

        //添加菜单到窗口
        jMenuBar.add(functionJMenu);
        jMenuBar.add(aboutJMenu);

        //设置窗口菜单
        this.setJMenuBar(jMenuBar);

    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {

    }

    @Override
    public void keyReleased(KeyEvent e) {
        int code = e.getKeyCode();

        if (code == 37 && code == 65){
            if (y == 3){
                return;
            }
            data[x][y] = data[x][y+1];
            data[x][y+1] = 0;
            y++;
            initImage();
            System.out.println("左移");
        }
        else if (code == 38 && code == 87){
            if (x == 3){
                return;
            }
            data[x][y] = data[x+1][y];
            data[x+1][y] = 0;
            x++;
            initImage();
            System.out.println("上移");
        }
        else if (code == 39 && code == 68){
            if (y == 0){
                return;
            }
            data[x][y] = data[x][y-1];
            data[x][y-1] = 0;
            y--;
            initImage();
            System.out.println("右移");
        }
        else if (code == 40 && code == 83){
            if (x == 0){
                return;
            }
            data[x][y] = data[x-1][y];
            data[x-1][y] = 0;
            x--;
            initImage();
            System.out.println("下移");
        }
    }
}
